package com.cskaoyan.service.promotion_service;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.AdExample;
import com.cskaoyan.bean.Storage;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.bean.vo.promotion_vo.AdCreateVO;
import com.cskaoyan.bean.vo.promotion_vo.AdUpdateVO;
import com.cskaoyan.bean.vo.promotion_vo.AdVO;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/7 13:56
 */
@Service
public class AdServiceImpl implements AdService{
    @Autowired
    AdMapper adMapper;

    @Autowired
    StorageMapper storageMapper;

    @Autowired
    AliyunComponent aliyunComponent;

    @Override
    public AdVO list(BaseParam param, String name, String content) {
        //从请求参数中获取参数
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        String sort = param.getSort();
        String order = param.getOrder();
        //开启分页
        PageHelper.startPage(page, limit);
        //SQL语句拼接
        AdExample example = new AdExample();
       AdExample.Criteria criteria = example.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        if (content != null && !"".equals(content)) {
            criteria.andContentLike("%"+content+"%");
        }
        example.setOrderByClause(sort + " " + order);
        //响应VO
        List<Ad> ads = adMapper.selectByExample(example);
        PageInfo<Ad> pageInfo = new PageInfo<>(ads);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        AdVO adVo = new AdVO();
        adVo.setTotal(total);
        adVo.setPages(pages);
        adVo.setLimit(limit);
        adVo.setPage(page);
        adVo.setList(ads);
        return adVo;
    }

    @Override
    public AdCreateVO create(Ad ad) {
        int selective = adMapper.insertSelective(ad);
        /**
         * 查询新增数据的id→查询出表中最大的id即是刚新增的id
         */
        Integer id = adMapper.selectMaxId();

        AdCreateVO adCreateVo = new AdCreateVO(id,
                ad.getName(),
                ad.getLink(),
                ad.getUrl(),
                ad.getPosition().intValue(),
                ad.getContent(),
                ad.getEnabled(),
                ad.getAddTime(),
                ad.getUpdateTime());

        return adCreateVo;
    }
//    @SneakyThrows
//    @Override
//    public Storage adImgStorage(MultipartFile file) {
//        String key = UUID.randomUUID().toString().replaceAll("-","") + ".png";
//        String name = file.getOriginalFilename();
//        String type = file.getContentType();
//        int size = (int)file.getSize();
//        String url = aliyunComponent.fileUpload(file);
//        Date date = new Date();
//
//        Storage storage = new Storage(null, key, name, type, size, url, date, date, false);
//        int insert = storageMapper.insert(storage);
//        storage.setDeleted(null);
//        return storage;
//    }

    @Override
    public void delete(Ad ad) {
        adMapper.deleteByPrimaryKey(ad.getId());
    }

    @Override
    public AdUpdateVO updateAD(Ad ad) {
        AdExample example = new AdExample();
        AdExample.Criteria criteria = example.createCriteria();
        ad.setUpdateTime(new Date());
        int update = adMapper.updateByPrimaryKeySelective(ad);
        if (update == 1) {
            AdUpdateVO adUpdateVO = new AdUpdateVO(ad.getId(),
                    ad.getName(),
                    ad.getLink(),
                    ad.getUrl(),
                    ad.getPosition().intValue(),
                    ad.getContent(),
                    ad.getEnabled(),
                    ad.getAddTime(),
                    ad.getUpdateTime(),
                    ad.getDeleted());
            return adUpdateVO;
        }
        return null;
    }
}
